package com.lody.virtual.helper.compat;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

import mirror.android.app.ActivityManager;
import mirror.android.app.ActivityManagerNative;
import mirror.android.app.IActivityManager;
import mirror.android.app.IActivityManagerICS;
import mirror.android.app.IActivityManagerL;
import mirror.android.app.IActivityManagerN;

/**
 * @author Lody
 */

public class ActivityManagerCompat {
	/** Type for IActivityManager.serviceDoneExecuting: anonymous operation */
	public static final int SERVICE_DONE_EXECUTING_ANON = 0;
	/** Type for IActivityManager.serviceDoneExecuting: done with an onStart call */
	public static final int SERVICE_DONE_EXECUTING_START = 1;
	/** Type for IActivityManager.serviceDoneExecuting: done stopping (destroying) service */
	public static final int SERVICE_DONE_EXECUTING_STOP = 2;

	public static final int START_SUCCESS = ActivityManager.START_SUCCESS == null ?
			0 : ActivityManager.START_SUCCESS.get();

	/**
	 * Result for IActivityManager.startActivity: an error where the
	 * given Intent could not be resolved to an activity.
	 */
    public static final int START_INTENT_NOT_RESOLVED = ActivityManager.START_INTENT_NOT_RESOLVED == null ?
            -1 : ActivityManager.START_INTENT_NOT_RESOLVED.get();

	/**
	 * Result for IActivityManager.startActivity: trying to start a background user
	 * activity that shouldn't be displayed for all users.
	 */
	public static final int START_NOT_CURRENT_USER_ACTIVITY = ActivityManager.START_NOT_CURRENT_USER_ACTIVITY == null ?
            -8 : ActivityManager.START_NOT_CURRENT_USER_ACTIVITY.get();

	/**
	 * Result for IActivityManaqer.startActivity: activity wasn't really started, but
	 * a task was simply brought to the foreground.
	 */
	public static final int START_TASK_TO_FRONT = ActivityManager.START_TASK_TO_FRONT == null ?
            2 : ActivityManager.START_TASK_TO_FRONT.get();

	/**
	 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
	 * for a sendBroadcast operation.
	 */
	public static final int INTENT_SENDER_BROADCAST = 1;

	/**
	 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
	 * for a startActivity operation.
	 */
	public static final int INTENT_SENDER_ACTIVITY = 2;

	/**
	 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
	 * for an activity result operation.
	 */
	public static final int INTENT_SENDER_ACTIVITY_RESULT = 3;

	/**
	 * Type for IActivityManaqer.getIntentSender: this PendingIntent is
	 * for a startService operation.
	 */
	public static final int INTENT_SENDER_SERVICE = 4;

	/** User operation call: success! */
	public static final int USER_OP_SUCCESS = 0;

	public static boolean finishActivity(IBinder token, int code, Intent data) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
			return IActivityManagerN.finishActivity.call(
					ActivityManagerNative.getDefault.call(),
					token, code, data, 0);
		} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			return IActivityManagerL.finishActivity.call(
						ActivityManagerNative.getDefault.call(),
						token, code, data, false);
		} else {
			IActivityManagerICS.finishActivity.call(
					ActivityManagerNative.getDefault.call(),
					token, code, data
			);
		}

		return false;
	}


    public static void setActivityOrientation(Activity activity, int orientation) {
        try {
            activity.setRequestedOrientation(orientation);
        } catch (Throwable e) {
            e.printStackTrace();
            //samsung is WindowManager.setRequestedOrientation
            Activity parent =  mirror.android.app.Activity.mParent.get(activity);
            while (parent != null) {
                parent =  mirror.android.app.Activity.mParent.get(parent);
            }
            IBinder token = mirror.android.app.Activity.mToken.get(parent);
            try {
                IActivityManager.setRequestedOrientation.call(ActivityManagerNative.getDefault.call(), token, orientation);
            }catch (Throwable ex){
                ex.printStackTrace();
            }
        }
    }

	public static String getPackageName(android.app.ActivityManager.RecentTaskInfo taskInfo) {
		if (taskInfo == null) {
			return null;
		}
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
			if (taskInfo.baseActivity != null) {
				return taskInfo.baseActivity.getPackageName();
			}
		} else {
			if (taskInfo.baseIntent != null) {
				return IntentCompat.getPackageName(taskInfo.baseIntent);
			}
		}
		return null;
	}
}
